package org.eoldroid.locateme;


import java.util.List;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MapiActivity extends MapActivity {
	MapView mapView;
	MyLocationOverlay mlo;
	List<Overlay> mapOverlays;
	
	private final int DIALOG_ABOUT = 0;
	private final int DIALOG_EXIT  = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
		
		mapView = (MapView) findViewById(R.id.mapiview);
		mapView.setBuiltInZoomControls(true);
		
		mapOverlays = mapView.getOverlays();
		mlo = new MyLocationOverlay(this.getApplicationContext(),mapView);

		if(!mlo.enableMyLocation()){
			Toast.makeText(this, R.string.toast, Toast.LENGTH_LONG).show();
			this.finish();
		}
		
		mlo.enableCompass();
		mapOverlays.add(mlo);
	}
    /** Called when the activity is first created. */
	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mlo.enableMyLocation();
		mlo.enableCompass();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mlo.disableMyLocation();
		mlo.disableCompass();
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(0, R.drawable.user, 0, "About").setIcon(R.drawable.user);
	    menu.add(0, R.drawable.exit, 1, "Exit").setIcon(R.drawable.exit);
	    return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.drawable.user:
	        showDialog(DIALOG_ABOUT);
	        return true;
	    case R.drawable.exit:
	        showDialog(DIALOG_EXIT);
	        return true;
	    }
	    return false;
	}

	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
	    switch(id) {
	    case DIALOG_ABOUT:
	        dialog = new Dialog(this);

	        dialog.setContentView(R.xml.custom_dilog);
	        dialog.setTitle(R.string.title);

	        TextView text = (TextView) dialog.findViewById(R.id.text);
	        text.setText(R.string.author);
	        ImageButton bok = (ImageButton) dialog.findViewById(R.id.image);
	        bok.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					dismissDialog(DIALOG_ABOUT);
				}
	        });
	        return dialog;
	    case DIALOG_EXIT:
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setMessage(R.string.confirm)
	    	       .setCancelable(false)
	    	       .setPositiveButton("Si", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                MapiActivity.this.finish();
	    	           }
	    	       })
	    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                dialog.cancel();
	    	           }
	    	       });
	    	return builder.create();
	    }
	    return dialog;
	}
}